package com.china08.yxyapi.api.manage.tec.teaching.mistakes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.res.YxyResBook;
import com.china08.yxyapi.entity.mg.lecture.YxyLectureCatalog;
import com.china08.yxyapi.entity.mg.mistakes.TeaCenterMistakes;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.repository.db.YxyResBookRepository;
import com.china08.yxyapi.repository.mg.lecture.YxyLectureCatalogRepository;
import com.china08.yxyapi.repository.mg.mistakes.TeaCenterMistakesRepository;
import com.china08.yxyapi.security.profile.YxyProfile;

/**
 * 教师中心任教书籍保存
 * 
 * @author Administrator
 *
 */
@CrossOrigin
@RestController
public class YxyMistakesCatalogCrtController extends YxyApiBaseController {

    @Autowired
    private YxyLectureCatalogRepository yxyLectureCatalogRepository;

    @Autowired
    private YxyResBookRepository yxyResBookRepository;

    @Autowired
    private TeaCenterMistakesRepository teaCenterMistakesRepository;

    @RequestMapping(value = "/api_yw/manage/tec/teaching/mistakes/mistakescatalog/crt", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel model) {
        YxyProfile yxyProfile = getProfile(request, response);
        String teaId = yxyProfile.getId();
        String schId = yxyProfile.getSchoolId();

        YxyResBook book = yxyResBookRepository.findById(model.bookId);
        YxyLectureCatalog yxyLectureCatalog;

        yxyLectureCatalog = yxyLectureCatalogRepository.findByUserIdAndSchoolIdAndBookId(teaId, schId, book.getId());
        if (yxyLectureCatalog == null) {
            yxyLectureCatalog = new YxyLectureCatalog();
            yxyLectureCatalog.setId(teaId + schId + book.getId());
            yxyLectureCatalog.setUserId(teaId);
            yxyLectureCatalog.setSchoolId(schId);
            yxyLectureCatalog.setBookId(book.getId());
            yxyLectureCatalog.setPhaseId(book.getPhaseId());
            yxyLectureCatalog.setPhaseName(book.getPhaseName());
            yxyLectureCatalog.setSubjectId(book.getSubjectId());
            yxyLectureCatalog.setSubjectName(book.getSubjectName());
            yxyLectureCatalog.setVersionId(book.getVersionId());
            yxyLectureCatalog.setVersionName(book.getVersionName());
            yxyLectureCatalog.setSemesterId(book.getSemersterId());
            yxyLectureCatalog.setSemesterName(book.getSemersterName());
            yxyLectureCatalogRepository.save(yxyLectureCatalog);
        }

        TeaCenterMistakes centerMistakes = teaCenterMistakesRepository.findOne(teaId + schId);
        if (centerMistakes == null) {
            centerMistakes = new TeaCenterMistakes();
            centerMistakes.setId(teaId + schId);
            centerMistakes.setTeacherId(teaId);
            centerMistakes.setSchoolId(schId);
        }
        centerMistakes.setStage(yxyLectureCatalog.getPhaseId());
        centerMistakes.setStageName(yxyLectureCatalog.getPhaseName());
        centerMistakes.setSubject(yxyLectureCatalog.getSubjectId());
        centerMistakes.setSubjectName(yxyLectureCatalog.getSubjectName());
        centerMistakes.setVersion(yxyLectureCatalog.getVersionId());
        centerMistakes.setVersionName(yxyLectureCatalog.getVersionName());
        centerMistakes.setBookId(yxyLectureCatalog.getBookId());
        centerMistakes.setBookName(yxyLectureCatalog.getSemesterName());
        centerMistakes.setBookContent(null);
        centerMistakes.setBookContentName(null);
        teaCenterMistakesRepository.save(centerMistakes);
    }

    /**
     * 请求参数
     * 
     */
    protected static class ReqModel extends YxyApiReqModel {
        // 册别id
        public String bookId;
    }
}
